﻿/**************************************************************
 *职位申请
 *
 *  Version              CreateBy                 CreateOn
 *  1.0                  hulei                    2012-11-25
 * 
 *****************************************************************/  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Cat.Domain;
using Forest.Cat.Domain.Services.Applications;
using Forest.Cat.Admin.Models;
using Forest.Cat.Admin.Models.Applications;
using Forest.Cat.Domain.Entities;
using Forest.Cat.Domain.Services.Compnaies;
using Forest.Cat.Domain.Services;

namespace Forest.Cat.Admin.Controllers
{
    public class ApplyHistoryController : BaseController
    {
        private readonly IWorkContext _workcontext;
        private readonly IApplicationService _applicationService;
        private readonly ICompanyService _companyService;
        private readonly IResumeService _resumeService;
        private const int _pageSize = 10;

        public ApplyHistoryController(IWorkContext workcontext, IApplicationService applicationService,
            ICompanyService companyService,IResumeService resumeService)
        {
            this._applicationService = applicationService;
            this._workcontext = workcontext;
            this._companyService = companyService;
            this._resumeService = resumeService;
        }

        public ActionResult List(PagingFilteringModel command)
        {
            var model = new ApplicationListModel();

            // 设置默认的Page信息
            if (command.PageSize <= 0) command.PageSize = 20;
            if (command.PageNumber <= 0) command.PageNumber = 1;

            //var id = _workcontext.CurrentUser.Id;
            var id = new Guid("00000000-0000-0000-0000-000001014331");
            var list = _applicationService.GetApplicationsByJobseekerId(id,
                command.PageNumber - 1, command.PageSize);
            model.PagingFilteringContext.LoadPagedList(list);

            model.Applications = list.Select(x =>
            {
                var m = EntityToViewModel(x);
                return m;
            }).ToList();

            return View(model);
        }

        private ApplicationModel EntityToViewModel(Application entity)
        {
            var companyItem = _companyService.GetCompanyById(entity.CompanyId);
            var resumeItem = _resumeService.GetResumeById(entity.ResumeId);
            return new ApplicationModel
            {
                Id = entity.Id,
                CompanyId = entity.CompanyId,
                CompanyName = entity.CompanyName,
                DateApplied = entity.DateApplied,
                JobTitle = entity.JobTitle,
                Location = entity.Location,
                PublishDays = DateTime.Now.Subtract(entity.StartDate).Days,
                ResumeId = entity.ResumeId,
                ResumeName = resumeItem!=null?resumeItem.ResumeName:string.Empty,
                Status = entity.Status,
                StatusName = entity.Status == 1 ? "已申请" : "已读取"
            };
        }

    }
}
